package com.mario.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mario.domain.Product;
import com.mario.repository.ProductDAO;

@Service
public class ProductManager {

	private static final long serialVersionUID = -3397863591379957635L;
	
	static Logger logger = Logger.getLogger(ProductManager.class);

	//******************************************************************
    // Attributes
    //******************************************************************
	
	@Autowired
	private ProductDAO productDAO;
	
	//******************************************************************
    // Contructor
    //******************************************************************
	
	public ProductManager() {
		super();
	}
	
	//******************************************************************
    // Getters and Setters
    //******************************************************************
	
    public ProductDAO getProductDAO() {
		return productDAO;
	}

	public void setProductDAO(ProductDAO productDAO) {
		this.productDAO = productDAO;
	}
	
	//******************************************************************
    // Methods
    //******************************************************************
	
	public List<Product> getProducts() {
		logger.info("Getting product List.");
        return productDAO.getAll();
    }

	public void addProduct(String description, Double price) {
    	logger.info("Adding Product " + description + " to product list.");
		Product product = new Product(description, price);
    	productDAO.persist(product);
	}

	public void deleteProduct(long productId) {
		logger.info("Deleting Product. ID: " + productId + ".");
		productDAO.delete(productDAO.load(productId));
	}

	public void updateProduct(Product product) {
		logger.info("Updating Product " + product.getDescription() + ".");
		productDAO.update(product);
	}
	
    public void increasePrice(int percentage) {
    	logger.info("Increasing product prices.");
    	
    	List<Product> products = productDAO.getAll();
    	
    	if (products != null)
	    	for (Product product : products) {
	    		double newPrice = product.getPrice().doubleValue() * (100 + percentage)/100;
	    		product.setPrice(newPrice);
	    		productDAO.update(product);
			}
    }
    
}

